//
//  GSCommonLoginButton.m
//  GSLoginDemo
//
//  Created by guoshikeji_a on 2018/7/27.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "GSCommonLoginButton.h"

#import "GSCommonLoginPrefixProfile.h"

#import "GSCommonLoginMMMaterialDesignSpinner.h"

@interface GSCommonLoginButton ()

@property (nonatomic, strong) GSCommonLoginMMMaterialDesignSpinner *designSpinner;


@end

@implementation GSCommonLoginButton

- (NSArray *)tintColors {
    return @[RGBHEX(0xFFFFFF),
             RGBHEX(0xFF6868),
             RGBHEX(0xF9E086),
             RGBHEX(0x7274FC),
             RGBHEX(0xE4CDAA)];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.layer.cornerRadius = [GSCommonLoginButton defaultSize].height/2;
        self.enabled = true;
        //
        
        CGFloat height = ceilf([GSCommonLoginButton defaultSize].height*(0.6));
        self.designSpinner = [GSCommonLoginMMMaterialDesignSpinner new];
        self.designSpinner.hidesWhenStopped = true;
        self.designSpinner.tintColors = [self tintColors];
        [self addSubview:self.designSpinner];
        [self.designSpinner mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self);
            make.height.width.mas_equalTo(height);
        }];
    }
    return self;
}

- (void)setButtonStatus:(GSCommonButtonStatus)buttonStatus {
    _buttonStatus = buttonStatus;
    //
    self.titleLabel.alpha = 1;
    self.imageView.alpha = 1;
    [self.designSpinner stopAnimating];
    switch (buttonStatus) {
        case GSCommonButtonStatusNormal:
        {
            
        }
            break;
        case GSCommonButtonStatusLoading: {
            self.titleLabel.alpha = 0;
            self.imageView.alpha = 0;
            [self.designSpinner startAnimating];
        }
            break;
        default:
            break;
    }
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.buttonStatus==GSCommonButtonStatusLoading) {
        return false;
    }
    return [super pointInside:point withEvent:event];
}

- (void)setEnabled:(BOOL)enabled {
    [super setEnabled:enabled];
    self.layer.shadowOpacity = 1;
    self.layer.shadowRadius = 9;
    self.layer.shadowOffset = CGSizeMake(0, 4.5);
    if (enabled) {
        self.backgroundColor = GSLOGIN_THEME_COLOR;
        self.layer.shadowColor = GSLOGIN_THEME_SHADOW_COLOR.CGColor;
    }else {
        self.backgroundColor = GSLOGIN_GRAY_COLOR;
        self.layer.shadowColor = GSLOGIN_GRAY_SHADOW_COLOR.CGColor;
    }
}


+ (CGSize)defaultSize {
    CGFloat width = GSLOGINScreenWidth*(355.0/375.0);
    return CGSizeMake(width, 42.0);
}
@end
